import 'dart:ui';
import 'package:flame/components/component.dart';
import 'package:hello_flutter/demo/game/game_controller.dart';

class HeathBar extends Component {
  final GameController controller;
  Rect heathBar; // 血条
  Rect remainBar; // 残血

  HeathBar(this.controller) {
    double barWidth = controller.screenSize.width / 1.75;
    heathBar = Rect.fromLTWH(
      controller.screenSize.width / 2 - barWidth / 2,
      controller.screenSize.height * 0.8,
      barWidth,
      controller.tileSize * 0.5,
    );
    remainBar = Rect.fromLTWH(
      controller.screenSize.width / 2 - barWidth / 2,
      controller.screenSize.height * 0.8,
      barWidth,
      controller.tileSize * 0.5,
    );
  }

  @override
  void render(Canvas canvas) {
    final heathBarColor = Paint()..color = Color(0xFFFF0000);
    final remainBarColor = Paint()..color = Color(0xFF00FF00);
    canvas.drawRect(heathBar, heathBarColor);
    canvas.drawRect(remainBar, remainBarColor);
  }

  @override
  void update(double time) {
    double barWidth = controller.screenSize.width / 1.75; // 血条宽度
    // 血量百分比
    double percentHeath =
        controller.player.currentHeath / controller.player.maxHeath;
    remainBar = Rect.fromLTWH(
      controller.screenSize.width / 2 - barWidth / 2,
      controller.screenSize.height * 0.8,
      barWidth * percentHeath,
      controller.tileSize * 0.5,
    );
  }
}
